#include "Base/ProcessorManager.h"
#include "Base/Log/LogDefine.h"

template <> CProcessorManager* CSingleton< CProcessorManager >::pInstance = new CProcessorManager();

CProcessorManager::CProcessorManager()
{
}


CProcessorManager::~CProcessorManager()
{
}

bool CProcessorManager::sendThreadMessage(CBaseProcessor *pProcessor, CThreadMessage *pMsg)
{
	if (pProcessor == nullptr || pMsg == nullptr)
	{
		return false;
	}

	pProcessor->receiveThreadMessage(pMsg);
	return true;
}

bool CProcessorManager::sendThreadMessage(int threadType, int threadIndex, CThreadMessage *pMsg)
{
	if (pMsg == nullptr)
	{
		return false;
	}

	PROCESSOR_HASH_MAP::iterator it = mProcessorHashMap.find((threadType * 1000 + threadIndex));
	if (it != mProcessorHashMap.end())
	{
		(it->second)->receiveThreadMessage(pMsg);
	}
	else
	{
		return false;
	}
	return true;
}

void CProcessorManager::addProcessor(int type, int index, CBaseProcessor* processor)
{
	if (processor == nullptr)
	{
		LOGW("processor is null");
		return;
	}

	int key = type * 1000 + index;

	mProcessorHashMap.insert(PROCESSOR_HASH_MAP::value_type(key, processor));
	mProcessorVector.push_back(processor);
}

CBaseProcessor* CProcessorManager::getProcessor(int type, int index)
{
	CBaseProcessor *processor = nullptr;
	PROCESSOR_HASH_MAP::iterator it = mProcessorHashMap.find((type * 1000 + index));
	if (it != mProcessorHashMap.end())
	{
		processor = (it->second);
	}

	return processor;
}
